package com.wash.shoes.common.config;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.wash.shoes.common.property.WxPayProperties;
import com.wash.shoes.util.AesUtil;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.refund.RefundService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;

/**
 * @author chilei
 * @description 微信支付配置类
 * @since 2023/7/20
 */
@Slf4j
@Configuration
@ConditionalOnClass(WxPayService.class)
public class WxPayConfiguration {

    @Resource
    private WxPayProperties wxPayProperties;

    @Autowired
    public WxPayConfiguration(WxPayProperties wxPayProperties) {
        this.wxPayProperties = wxPayProperties;
    }

    @Bean
    @ConditionalOnMissingBean
    public WxPayService wxService() {
        WxPayService wxPayService = new WxPayServiceImpl();
        if (StringUtils.isNotBlank(this.wxPayProperties.getAppid())) {
            wxPayService.setConfig(wxPayConfig());
        }
        return wxPayService;
    }

    @Bean
    public WxPayConfig wxPayConfig() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setKeyPath(this.wxPayProperties.getPrivateKeyPath());
        payConfig.setMchKey(this.wxPayProperties.getMchKey());
        payConfig.setAppId(StringUtils.trimToNull(this.wxPayProperties.getAppid()));
        payConfig.setMchId(StringUtils.trimToNull(this.wxPayProperties.getMchId()));
        payConfig.setPrivateKeyContent(this.wxPayProperties.getPrivateKeyContent().getBytes(StandardCharsets.UTF_8));
        payConfig.setPrivateCertString(this.wxPayProperties.getPrivateCertContent());
        payConfig.setApiV3Key(this.wxPayProperties.getApiV3key());
        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);
        return payConfig;
    }

    /**
     * @return AesUtil 返回加密的数据
     */
    @Bean
    public AesUtil aesUtilInit() {
        return new AesUtil(this.wxPayProperties.getApiV3key().getBytes());
    }

    /**
     * RefundService
     */
    @Bean
    public RefundService refundService() throws IOException, URISyntaxException {

        try {
            Config config = new RSAAutoCertificateConfig.Builder()
                    .merchantId(this.wxPayProperties.getMchId())
                    .privateKeyFromPath("/zzz/shoesWash/app/apiclient_key.pem")
                    .privateKey(this.wxPayProperties.getPrivateKeyContent())
                    .merchantSerialNumber(this.wxPayProperties.getMchKey())
                    .apiV3Key(this.wxPayProperties.getApiV3key())
                    .build();
            return new RefundService.Builder().config(config).build();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            Config config = new RSAAutoCertificateConfig.Builder()
                    .merchantId(this.wxPayProperties.getMchId())
                    .privateKeyFromPath(getPath(this.wxPayProperties.getPrivateKeyPath()))
                    .privateKey(this.wxPayProperties.getPrivateKeyContent())
                    .merchantSerialNumber(this.wxPayProperties.getMchKey())
                    .apiV3Key(this.wxPayProperties.getApiV3key())
                    .build();
            return new RefundService.Builder().config(config).build();
        }
    }

    public String getPath(String fileName) {
        String path = this.getClass().getClassLoader().getResource("").getPath();//注意getResource("")里面是空字符串
        String filePath = path + fileName;
        return filePath;
    }
}
